﻿namespace Ngs.Data
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using Ngs.Reflection;
    #endregion

    public class DataQueryProvider : IQueryProvider
    {
        // private IQueryProvider provider;

        public DataQueryProvider()
        {
            //this.provider = query.Provider;
        }


        #region IQueryProvider Members

        public IQueryable<TResult> CreateQuery<TResult>(Expression expression)
        {
            //if (expression == null)
            //{
            //    throw new ArgumentNullException("expression");
            //}
            //if (!typeof(IQueryable<TResult>).IsAssignableFrom(expression.Type))
            //{
            //    throw new ArgumentException("expression", typeof(IEnumerable<TResult>).ToString());
            //}
            //return new DataQuery<TResult>(this, expression);
            throw new NotImplementedException();
        }

        public IQueryable CreateQuery(Expression expression)
        {
            Type elementType = TypeHelper.GetElementType(expression.Type);
            try
            {
                return (IQueryable)Activator.CreateInstance(typeof(DataQuery<>).MakeGenericType(elementType), new object[] { this, expression });
            }
            catch (TargetInvocationException tie)
            {
                throw tie.InnerException;
            }
        }

        public TResult Execute<TResult>(Expression expression)
        {
            return (TResult)this.Execute(expression);
            // return (TResult)this.provider.Execute(expression);
        }

        public object Execute(Expression expression)
        {
            return null;
            //return this.provider.Execute(expression);
        }

        #endregion
    }
}

